/*
 * Copyright 2012-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.jackson;

import java.util.EnumMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.SerializationFeature;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 配置 Jackson 的配置属性。
 *
 * @author Andy Wilkinson
 * @author Marcel Overdijk
 * @author Johannes Edmeier
 * @since 1.2.0
 */
@ConfigurationProperties(prefix = "spring.jackson")
public class JacksonProperties {

	/**
	 * 日期格式字符串或完全限定的日期格式类名。例如，'yyyy-MM-dd HH:mm:ss'。
	 */
	private String dateFormat;

	/**
	 * Jackson 的 PropertyNamingStrategies 中的常量之一。也可以是 PropertyNamingStrategy 实现的完全限定类名。
	 */
	private String propertyNamingStrategy;

	/**
	 * Jackson 可见性阈值，可用于限制哪些方法（和字段）被自动检测。
	 */
	private final Map<PropertyAccessor, JsonAutoDetect.Visibility> visibility = new EnumMap<>(PropertyAccessor.class);

	/**
	 * 影响 Java 对象序列化方式的 Jackson 开/关特性。
	 */
	private final Map<SerializationFeature, Boolean> serialization = new EnumMap<>(SerializationFeature.class);

	/**
	 * 影响 Java 对象反序列化方式的 Jackson 开/关特性。
	 */
	private final Map<DeserializationFeature, Boolean> deserialization = new EnumMap<>(DeserializationFeature.class);

	/**
	 * Jackson 通用开/关特性。
	 */
	private final Map<MapperFeature, Boolean> mapper = new EnumMap<>(MapperFeature.class);

	/**
	 * 针对解析器的 Jackson 开/关特性。
	 */
	private final Map<JsonParser.Feature, Boolean> parser = new EnumMap<>(JsonParser.Feature.class);

	/**
	 * 针对生成器的 Jackson 开/关特性。
	 */
	private final Map<JsonGenerator.Feature, Boolean> generator = new EnumMap<>(JsonGenerator.Feature.class);

	/**
	 * 控制序列化期间属性的包含。使用 Jackson 的 JsonInclude.Include 枚举中的一个值进行配置。
	 */
	private JsonInclude.Include defaultPropertyInclusion;

	/**
	 * 宽松度的全局默认设置（如果有）。
	 */
	private Boolean defaultLeniency;

	/**
	 * 用于自动检测构造函数的策略，特别是单参数构造函数的行为。
	 */
	private ConstructorDetectorStrategy constructorDetector;

	/**
	 * 格式化日期时使用的时区。例如，“America/Los_Angeles”或“GMT+10”。
	 */
	private TimeZone timeZone = null;

	/**
	 * 用于格式化的区域设置。
	 */
	private Locale locale;

	public String getDateFormat() {
		return this.dateFormat;
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}

	public String getPropertyNamingStrategy() {
		return this.propertyNamingStrategy;
	}

	public void setPropertyNamingStrategy(String propertyNamingStrategy) {
		this.propertyNamingStrategy = propertyNamingStrategy;
	}

	public Map<PropertyAccessor, JsonAutoDetect.Visibility> getVisibility() {
		return this.visibility;
	}

	public Map<SerializationFeature, Boolean> getSerialization() {
		return this.serialization;
	}

	public Map<DeserializationFeature, Boolean> getDeserialization() {
		return this.deserialization;
	}

	public Map<MapperFeature, Boolean> getMapper() {
		return this.mapper;
	}

	public Map<JsonParser.Feature, Boolean> getParser() {
		return this.parser;
	}

	public Map<JsonGenerator.Feature, Boolean> getGenerator() {
		return this.generator;
	}

	public JsonInclude.Include getDefaultPropertyInclusion() {
		return this.defaultPropertyInclusion;
	}

	public void setDefaultPropertyInclusion(JsonInclude.Include defaultPropertyInclusion) {
		this.defaultPropertyInclusion = defaultPropertyInclusion;
	}

	public Boolean getDefaultLeniency() {
		return this.defaultLeniency;
	}

	public void setDefaultLeniency(Boolean defaultLeniency) {
		this.defaultLeniency = defaultLeniency;
	}

	public ConstructorDetectorStrategy getConstructorDetector() {
		return this.constructorDetector;
	}

	public void setConstructorDetector(ConstructorDetectorStrategy constructorDetector) {
		this.constructorDetector = constructorDetector;
	}

	public TimeZone getTimeZone() {
		return this.timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

	public Locale getLocale() {
		return this.locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public enum ConstructorDetectorStrategy {

		/**
		 * 使用启发式方法来判断是否使用“properties”模式。
		 */
		DEFAULT,

		/**
		 * 如果没有明确的注解，则假定使用“properties”模式。
		 */
		USE_PROPERTIES_BASED,

		/**
		 * 如果没有明确的注解，则假定使用“delegating”模式。
		 */
		USE_DELEGATING,

		/**
		 * 拒绝决定隐式模式，而是为不明确的情况抛出 InvalidDefinitionException。
		 */
		EXPLICIT_ONLY;

	}

}
